<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Session;
class User extends Controller{
    /****
     Class register           个人中心头部
     *@
    */  
    public function head(){
        $user_id = input('user_id');
        $user = Db::name('user')
            ->where(['id'=>$user_id])
            ->find();
        $collect_goods = Db::name('collect_goods')
                  ->where(['user_id'=>$user_id])
                  ->count();
        $collect_manystore = Db::name('collect_manystore')
                  ->where(['user_id'=>$user_id])
                  ->count();
        $browse = Db::name('browse')
                  ->where(['user_id'=>$user_id])
                  ->count();
        
        if($user){
           $arr = [
               'state'              => 1,
               'collect_goods'      =>$collect_goods,
               'collect_manystore'  =>$collect_manystore,
               'browse'             =>$browse,
               'user'               =>$user,
               'message'            => "请求成功"
            ];
        }else{
            $arr = [
               'state'     => 0,
               'session'  =>$user_id,
               'message'   => "用户不存在"
            ]; 
        }
        return json($arr);
    
  }


    /****
     Class register           修改
     *@
    */
    public function head_add(){
      $post = input('post.');
      $image = ROOT_PATH . 'public' . DS . 'uploads'.DS. 'user' .DS.time().'.png';
        file_put_contents($image, base64_decode($post['avatar']));
      $image_url = 'http://take.zhitiaoxiaoyuan.com/uploads/user/'.time().'.png';
      if($post['avatar'] != null){
         $data = [
          'avatar'=>$image_url,
        ];
      }
      if($post['nickname'] != null){
          $data = [
            'nickname'=>$post['nickname'],
          ];
      }
      if($post['gender'] != null){
          $data = [
            'gender'=>$post['gender'],
          ];
      }
      if($post['mobile'] != null){
          $data = [
            'mobile'=>$post['mobile'],
          ];
      }
      if($post['school'] != null){
          $data = [
            'school'=>$post['school'],
          ];
      }
     $user = Db::name('user')->where('id',$post['user_id'] )->update($data);
     if($user){
        $arr = [
           'state'     => 1,
           'message'   => "请求成功"
        ];
     }else{
      $arr = [
         'state'     => 0,
         'message'   => "请求失败"
        ];
     }
     return json($arr);
    }

    public function statistics(){
        $user_id = input('user_id');
        $statistics = Db::name('statistics')
                    ->where(['user_id'=>$user_id])
                    ->find();
        $general_income = Db::name('statistics_log')
                        ->where(['user_id'=>$user_id,'type'=>1])
                        ->sum('money');
        $today=strtotime(date('Y-m-d 00:00:00'));
        $statistics_log_1 = Db::name('statistics_log')
                        ->where(['user_id'=>$user_id,'type'=>1])
                        ->whereTime('addtime','>=',$today)
                        ->sum('money');
        $statistics_log_2 = Db::name('statistics_log')
        ->where(['user_id'=>$user_id,'type'=>2])
        ->whereTime('addtime','>=',$today)
        ->sum('money');
        if($statistics){
           $arr = [
               'state'                => 1,
               'general_income'       =>$general_income,
               'statistics'           =>$statistics,
               'statistics_log_1'     =>$statistics_log_1,
               'statistics_log_2'     =>$statistics_log_2,
               'message'              => "请求成功"
            ];
        }else{
            $arr = [
               'state'                => 0,
               'statistics'           =>null,
               'statistics_log_1'     =>0,
               'statistics_log_2'     =>0,
               'message'              => "全部为0"
            ]; 
        }
        return json($arr);
    }



       /****
     Class register           银行卡管理
     *@
    */
    public function account(){
      $user_id = input('user_id');
      $account_list = Db::name('withdraw_account a')
                    ->where(['a.user_id'=>$user_id])
                    ->join('bank b','a.bankname = b.bankname','LEFT')
                    ->select();
      $arr = [
         'state'        => 1,
         'account_list' => $account_list,
         'message'      => "请求成功"
      ];
      return json($arr);
    }
       /****
     Class register           提现记录
     *@
    */
    public function withdraw(){
      $post = 1;
      $data = [];
      for ($t=1; $t<=12; $t++) {
          // echo "：$t <br>";
      } 
      // // $today=strtotime(date('Y-m-d 00:00:00'));
      //  // if($post['type'] == 0){
      // $year=strtotime(date('Y-01-01 00:00:00'));
      // $withdraw = Db::name('withdraw')
      //           ->where(['user_id'=>$post])
      //           // ->field("FROM_UNIXTIME(createtime,'%Y-%m') as months,createtime,money")
      //           ->whereTime('createtime','>=',$year)
      //           ->select();
      //                       // ->order('addtime desc')
      //           // ->paginate(3);
      //   // }else{
      //   //     $statistics_log = Db::name('statistics_log')
      //   //                     ->where(['user_id'=>$post['user_id'],'type'=>$post['type']])
      //   //                     // ->order('addtime desc')
      //   //                     ->paginate(8);
         
      //   // }
      //    $arr = [
      //      'state'                  => 1,
      //      'withdraw'         => $withdraw,
      //      'message'                => "请求成功11"
      //   ];
      //   return json($arr);
    }
}